package com.xs.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xs.bean.UserBean;
import com.xs.dao.UserDao;
import com.xs.util.JdbcUtil;

public class UserDaoImpl extends JdbcUtil implements UserDao {

	// 登录方法
	@Override
	public UserBean login(String name, String ps) {
		// 这里我的登录用户跟密码都是String类型,所以可以用字符串拼接,如果有其他类型,需要转型
		String sql = "select * from user_tb where login_name =? and login_ps =?";
		String[] param = { name, ps };
		ResultSet rs = query2(sql, param);
		UserBean ub = null;
		try {
			while (rs.next()) {
				// 调用了UserBean里有参构造方法,注意类型的一一对应
				// 用while循环得到了用户的所有信息,然后通过索引来获得查询结果集中的的值。
				// rs.getInt(1)等价于rs.getInt("userId");rs.getString(2)等价于rs.getInt("loginName")
				ub = new UserBean(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getInt(5),
						rs.getString(6), rs.getInt(7), rs.getInt(8));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ub;
	}

	// 查询所有用户
	@Override
	public List<UserBean> getUserInfo(int  objectId) {
		String sql = "select * from user_tb where object_id = " + objectId;
		ResultSet rs = query(sql);
		List list = new ArrayList();
		try {
			while (rs.next()) {
				UserBean ub = new UserBean(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),
						rs.getInt(5), rs.getString(6), rs.getInt(7), rs.getInt(8));
				list.add(ub);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	// 查询当前用户
		@Override
		public UserBean getUserInfoById(int  objectId) {
			String sql = "select * from user_tb where object_id = " + objectId;
			ResultSet rs = query(sql);
			UserBean ub = null;
			try {
				while (rs.next()) {
					 ub = new UserBean(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),
							rs.getInt(5), rs.getString(6), rs.getInt(7), rs.getInt(8));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return ub;
		}

}
